//
//  MagiMapView.m
//  HelloTaxi
//
//  Created by zhenhua zhou on 12-7-17.
//  Copyright (c) 2012年 ECUST. All rights reserved.
//

#import "MagiMapView.h"

@implementation MagiMapView

@synthesize MapView = mMap;



-(void) drawRoute:(RouteData *)route
{
    NSMutableArray * encodeLine = [[[NSMutableArray alloc] init] autorelease];
   
    for(Segment * curSeg in route.Segments)
    {
        for(Waypoint *curPoint in curSeg.Waypoints)
        {
            NSMutableArray * curLineData = [self decodeLine:curPoint.PointsData];
            [encodeLine addObjectsFromArray:curLineData];
        }
    }
    
    int count = [encodeLine count];
    
    CLLocationCoordinate2D * pos = (CLLocationCoordinate2D* )malloc(sizeof(CLLocationCoordinate2D) * count );
    int index = 0;
    for( CLLocation * curLoc in encodeLine)
    {
        pos[index] = curLoc.coordinate;
        index++;
    }
    
    MKPolyline * line = [MKPolyline polylineWithCoordinates:pos count:count];
    [mMap addOverlay:line];
    
    
    
 //   [line release];
    
}

-(void) setMapPos:(GeoPos * )center armong:(GeoPos *)minPos at:(GeoPos *)maxPos
{
    MKCoordinateRegion region;
    region.center.latitude = (maxPos.Lat + minPos.Lat)/2;
    region.center.longitude = (maxPos.Lon + minPos.Lon)/2;
    region.span.latitudeDelta = maxPos.Lat - minPos.Lat;
    region.span.longitudeDelta = maxPos.Lon - minPos.Lon;
    [mMap setRegion:region animated:YES];
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    if([overlay isKindOfClass:[MKPolyline class]])
    {
    MKPolylineView* view = [[[MKPolylineView alloc] initWithOverlay:overlay] autorelease];
    view.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.3];
    view.strokeColor = [[UIColor blueColor]colorWithAlphaComponent:0.7];
        view.lineWidth = 6.0;
   //     mView.frame = mMap.frame;
        
        return view;
    }
 //   [view
    return nil;
}

-(void)clearRoutes
{
    for (id overlay in [mMap overlays])
    {
		if ([overlay isKindOfClass:[MKPolyline class]])
        {						
            [mMap removeOverlay:overlay];
        }
    }
//    [mMap removeOverlay:mRoute];
//    [mRoute release];
//    mRoute = nil;
//    [mView release];
//    mView = nil;
}

-(NSMutableArray *) decodeLine:(NSString *)data
{
    NSInteger len = [data length];
    NSInteger index = 0;
    NSMutableArray *re = [[[NSMutableArray alloc] init]autorelease];
    NSInteger lat = 0;
    NSInteger lon = 0;
    while (index <len) {
        NSInteger b;
        NSInteger shift = 0;
        NSInteger result = 0;
        do {
            b = [data characterAtIndex:index++] - 63;
            result |=(b & 0x1f)<< shift;
            shift +=5;
        } while (b>=0x20);
        NSInteger dlat = ((result & 1)? ~(result>>1):(result >> 1));
        lat += dlat;
        shift = 0;
        result = 0;
        
        do {
            b = [data characterAtIndex:index++] - 63;
            result |=(b & 0x1f)<< shift;
            shift +=5;
        } while (b>=0x20);
        NSInteger dlon = ((result & 1)? ~(result>>1):(result >> 1));
        lon += dlon;
        
        NSNumber * latitude = [[[NSNumber alloc]initWithFloat:lat * 1e-5]autorelease];
        NSNumber * longitude = [[[NSNumber alloc]initWithFloat:lon * 1e-5]autorelease];
        
        CLLocation *loc = [[[CLLocation alloc]initWithLatitude:[latitude floatValue] longitude:[longitude floatValue] ]autorelease];
        
        [re addObject:loc];

    }
    
    return re;
}

@end
